<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2016-10-15
 * Time: 17:10
 */

namespace app\src\user\logic;


use app\src\base\logic\BaseLogic;
use app\src\user\model\Member;
use app\src\extend\Page;
use app\src\user\model\UserRelation;
use think\Db;
class UserRelationLogic extends BaseLogic
{
    /**
     * @return mixed
     */
    protected function _init()
    {
        $this->setModel(new UserRelation());
    }



    public function check_or_same($my_uid,$my_or_type,$my_or_upper_uid){


        $relation=$this->getModel();
        $map['upper_uid']=$my_uid;
        $map['lower_type']=$my_or_type;
        //"itboye_user_member.goods_uid=".
        $on='itboye_user_member.uid=ur.lower_uid AND itboye_user_member.goods_uid='.$my_or_upper_uid;
        $list=$relation->alias('ur')->where($map)
            ->join('itboye_user_member',$on)
            //->field('um.*')
            ->select();

        if(!empty($list)){
        $list=json_encode($list);
        $list=json_decode($list,true);
        }
        return(['status'=>true,'info'=>$list]);
    }






    //admin
    public function relation_detail($admin_uid_map,$type,$page = array('curpage'=>0,'size'=>10)){
        $model=$this->getModel();
        $map['upper_uid']=$admin_uid_map;
        $map['lower_type']=['<>',4];
        if(!empty($type)) $map['lower_type']=$type;
        $list=$model->alias('ur')
            ->where($map)
            ->join(['common_member'=>'cm'],'(cm.uid) = (ur.lower_uid)')
            ->field('ur.*,cm.nickname,cm.head,cm.update_time')
            ->select();

        if(!empty($list)){
            $list=json_encode($list);
            $list=json_decode($list,true);
        }
        return(['status'=>true,'info'=>$list]);
    }

    //admin
    public function gethead($map){
        $model=$this->getModel();
        $list=$model->alias('ur')
            ->where($map)
            ->join(['itboye_platform'=>'p'],'(p.uid) = (ur.upper_uid)')
            ->join(['itboye_user_member'=>'um'],'(ur.upper_uid) = (um.uid) AND um.type=1')
            ->join(['common_member'=>'cm'],'(cm.uid) = (p.id)','left')
            ->field('ur.*,cm.nickname,cm.head,cm.update_time')
            ->find();

        if(!empty($list)){
            $list=json_encode($list);
            $list=json_decode($list,true);
        }
        return(['status'=>true,'info'=>$list]);
    }

}